<template>
  <div class="p-2">
    <!--查询区域-->
    <div class="jeecg-basic-table-form-container">
      <a-form ref="formRef" @keyup.enter.native="searchQuery" :model="queryParam" :label-col="labelCol" :wrapper-col="wrapperCol">
		<a-row :gutter="24">
          <a-col :xl="6">
			<a-divider orientation="left">分类日程</a-divider>
            <a-form-item name="title">
              <j-checkbox type="checkbox" v-model:value="queryParam.type" dictCode="scheduleType" placeholder="请选择日程类型"  allow-clear />
            </a-form-item>
			<a-divider orientation="left"></a-divider>
			<a-alert :message="`您选中的时间为: ${selectedValue}`" />
			<a-divider orientation="left"></a-divider>
			<a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增日程</a-button>
          </a-col>
          
          <a-col :xl="18">
			  <!--日历内容-->
			  <div class="jeecg-basic-table-form-container">
			    <a-calendar v-model:value="currDate" @select="onSelect" @panelChange="handleAdd">
			      <template #dateCellRender="{ current }">
			        <ul class="events">
			          <li v-for="item in getListData(current)" :key="item.content">
			            <a-badge :status="item.type" :text="item.content" />
			          </li>
			        </ul>
			      </template>
			  		 <template #monthCellRender="{ current }">
			  		   <div v-if="getMonthData(current)" class="notes-month">
			  			 <section>{{ getMonthData(current) }}</section>
			  			 <span>Backlog number</span>
			  		   </div>
			  		 </template>
			    </a-calendar>
			  </div>
          </a-col>
        </a-row>
      </a-form>
    </div>
     
     <!-- 表单区域 -->
     <TbPlanModal ref="registerModal" @success="handleSuccess"></TbPlanModal>
  </div>
</template>

<script lang="ts" name="TbPlan-tbPlan" setup>
  import { ref, reactive,onMounted } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { columns, superQuerySchema } from './TbPlan.data';
  import { list, deleteOne, batchDelete, getImportUrl, getExportUrl,selectPlanList } from './TbPlan.api';
  import { downloadFile } from '/@/utils/common/renderUtils';
  import TbPlanModal from './components/TbPlanModal.vue'
  import { useUserStore } from '/@/store/modules/user';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JSwitch from '/@/components/Form/src/jeecg/components/JSwitch.vue';
  import JSelectUser from '/@/components/Form/src/jeecg/components/JSelectUser.vue';
  import JCheckbox from "/@/components/Form/src/jeecg/components/JCheckbox.vue";
  import dayjs from 'dayjs';
  
  const formRef = ref();
  const queryParam = reactive<any>({});
  const toggleSearchStatus = ref<boolean>(false);
  const registerModal = ref();
  const userStore = useUserStore();
  //注册table数据
  const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: '日程、计划',
      api: list,
      columns,
      canResize:false,
      useSearchForm: false,
      actionColumn: {
        width: 120,
        fixed: 'right',
      },
      beforeFetch: (params) => {
        return Object.assign(params, queryParam);
      },
    },
    exportConfig: {
      name: "日程、计划",
      url: getExportUrl,
      params: queryParam,
    },
	  importConfig: {
	    url: getImportUrl,
	    success: handleSuccess
	  },
  });
  const [registerTable, { reload, collapseAll, updateTableDataRecord, findTableDataRecord, getDataSource }, { rowSelection, selectedRowKeys }] = tableContext;
  const labelCol = reactive({
    xs:24,
    sm:4,
    xl:6,
    xxl:4
  });
  const wrapperCol = reactive({
    xs: 24,
    sm: 20,
  });
  const currDate = dayjs();
  const planListData: any = ref([]);
  const selectedValue = ref(dayjs(new Date()).format('YYYY-MM-DD'));
  
  function onSelect(value){
    selectedValue.value = dayjs(value).format('YYYY-MM-DD');
  }

  /**
   * 新增事件
   */
  function handleAdd() {
    registerModal.value.disableSubmit = false;
    registerModal.value.add();
  }
  
  /**
   * 编辑事件
   */
  function handleEdit(record: Recordable) {
    registerModal.value.disableSubmit = false;
    registerModal.value.edit(record);
  }
   
  /**
   * 详情
   */
  function handleDetail(record: Recordable) {
    registerModal.value.disableSubmit = true;
    registerModal.value.edit(record);
  }
   
  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, handleSuccess);
  }
   
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
  }
   
  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }
   
  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
      },
    ];
  }
   
  /**
   * 下拉操作栏
   */
  function getDropDownAction(record) {
    return [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      }, {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
          placement: 'topLeft',
        }
      }
    ]
  }

  /**
   * 查询
   */
  function searchQuery() {
    reload();
  }
  
  /**
   * 重置
   */
  function searchReset() {
    formRef.value.resetFields();
    selectedRowKeys.value = [];
    //刷新数据
    reload();
  }
  

  /**
   * form点击事件(以逗号分割)
   * @param key
   * @param value
   */
  function handleFormJoinChange(key, value) {
    if (typeof value != 'string') {
      queryParam[key] = value.join(',');
    }
  }
  
  // 自动请求并暴露内部方法
  onMounted(() => {
    getPlanListData();
  });
  
  async function getPlanListData() {
	  await selectPlanList({}).then((responseResult) => {
		planListData.value = responseResult;
	  });
	}
  
  function getMonthData(currDate){
	  if (currDate.month() === 8) {
	    return 1394;
	  }
  }
  
  function getListData(currDate) {
      interface MyItem {
        type: string;
        content: string;
      }
  
  
      let listData: MyItem[] = [];
      planListData.value?.forEach((item) => {
        if (dayjs(item.startTime).format('D') == currDate.date()) {
          listData.push({ type: 'warning', content: item.title });
        }
      });
      return listData;
    }
  
</script>

<style lang="less" scoped>
  .jeecg-basic-table-form-container {
    padding: 0;
    .table-page-search-submitButtons {
      display: block;
      margin-bottom: 24px;
      white-space: nowrap;
    }
    .query-group-cust{
      min-width: 100px !important;
    }
    .query-group-split-cust{
      width: 30px;
      display: inline-block;
      text-align: center
    }
    .ant-form-item:not(.ant-form-item-with-help){
      margin-bottom: 16px;
      height: 32px;
    }
    :deep(.ant-picker),:deep(.ant-input-number){
      width: 100%;
    }
  }
</style>
